package com.zj.base.httpclien;

import android.support.annotation.NonNull;

/**
 * Created by zhaojie on 2018/3/27.
 */

public abstract class HttpRequestBuilder<T> {

    public HttpRequestBuilder(int reqCode, HttpClient<T> client) {
        this.client = client;
        this.reqCode = reqCode;
    }

    protected final int reqCode;
    protected final HttpClient<T> client;

    public enum HttpMethod {
        get, post
    }

    abstract HttpMethod getMethod();

    /**
     * @param getEnable  当前缓存执行的条件过滤，是否在获取网络数据前先载入本地数据
     * @param saveEnable 当前缓存执行的条件过滤，是否在获取网络数据前后刷新本地数据
     */
    public HttpRequestBuilder<T> cacheEnable(boolean getEnable, boolean saveEnable) {
        client.setCacheGetEnable(getEnable);
        client.setCacheSaveEnable(saveEnable);
        return this;
    }

    public HttpRequestBuilder<T> dataType(@NonNull HttpClient.DataType type) {
        if (type == HttpClient.DataType.CACHE)
            throw new IllegalStateException("the type<Cache> was occupy by httpClient self ,it`ll callback when you set \"cache enable true\" ");
        client.setDataType(type);
        return this;
    }

    public void send() {
        client.handleData(this, reqCode);
    }
}
